package com.liu.resume.ui.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import org.greenrobot.eventbus.EventBus;

import common.pie.utils.ToastUtil;
import common.widget.LoadingDialog;

/**
 */
public class BaseFragment extends Fragment {

    protected LoadingDialog mLoadingDialog;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ToastUtil.setContext(getActivity());
    }


    public void startActivity(Intent intent) {
        getActivity().startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isRegisterEventBus)
            EventBus.getDefault().unregister(this);
    }

    private boolean isRegisterEventBus = false;//判断是否进行EventBus的注册，用于取消注册时的判断

    /**
     * 注册EventBus ，如果调用此方法，必须要声明接收函数，否则报错
     * onEvent(AnyTypeEvent event)/onEventMainThread(AnyTypeEvent event)..
     * 具体参见EventBus使用介绍http://blog.csdn.net/harvic880925/article/details/40787203
     */
    protected void registerEventBus() {
        //如果尚未注册，先注册，避免重复注册
        if (!isRegisterEventBus) {
            EventBus.getDefault().register(this);
            isRegisterEventBus = true;
        }
    }

    /**
     * 处理加载
     */
    public void showLoadingDialog(String message) {
        this.showLoadingDialog(message, true, true);
    }

    public void showLoadingDialog(String message, boolean cancelable, boolean otoCancelable) {
        mLoadingDialog = new LoadingDialog(this.getActivity(), message, cancelable, otoCancelable);
        mLoadingDialog.show();
    }

    /**
     * 关闭进度条
     */
    public void dismissLoadingDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftInput() {
        /** 隐藏软键盘 **/
        View view = getActivity().getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
